@extends('layout.page')
@section('title','发送短息')
@section('stylesheet')

    <!-- Sweet Alert -->
    <link href="/css/plugins/sweetalert/sweetalert.css" rel="stylesheet">
@endsection

@section('static-page')
    <div class="col-sm-12">
        <div class="ibox float-e-margins">
            <div class="ibox-title">
                <h5>营销短信 <small>&nbsp;&nbsp;短信剩余</small><span class="text-danger">{{ $surplus }}</span>条 </h5>
            </div>
            <div class="ibox-content">
                <form method="post" action="/ceb/channels/store" class="form-horizontal">
                    {{csrf_field()}}

                    <div class="form-group">
                        <label class="col-sm-2 control-label">短信内容：</label>
                        <div class="col-sm-6">
                            <div class="row">
                                <div class="col-md-8">
                                    <span class="help-block m-b-none text-danger">（*内容长度不大于350个汉字,内容必须带有尊称和"回复TD退订"信息!）</span>
                                    <textarea name="sms-content" class="form-control"
                                              placeholder="短信内容" rows="5" maxlength="350"></textarea>
                                    <span id="text-count" class="help-block m-b-none"></span>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="hr-line-dashed"></div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" >渠道：</label>
                        <div class="col-sm-10">
                            <div class="row">
                                <div class="col-sm-10">
                                    <fieldset>
                                        <legend >
                                            <small>
                                                <label class="checkbox-inline i-checks">
                                                    <input class="check-all"
                                                           type="checkbox" data-value=""
                                                    >
                                                    全选
                                                </label>
                                            </small>
                                        </legend>
                                        @foreach($channelList as $child)
                                            <label class="checkbox-inline i-checks">
                                                <input class="check-all-child" type="checkbox" name="channels[]" value="{{$child['no']}}"

                                                >{{$child['channel_provider_name']}}</label>
                                        @endforeach
                                    </fieldset>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="hr-line-dashed"></div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label">注册时间：</label>
                        <div class="col-sm-10">
                            <input readonly placeholder="开始日期" name="info[reg_start_time]" class="form-control layer-date" id="start">
                            ~~
                            <input readonly placeholder="结束日期" name="info[reg_end_time]" class="form-control layer-date" id="end">
                        </div>
                    </div>

                    <div class="form-group ">
                        <label class="col-sm-2 control-label">实名：</label>
                        <div class="col-sm-2">
                            <select class="form-control m-b" name="info[realname_status]">
                                @foreach($realnamed as $key => $val)
                                    <option value="{{ $val['id'] }}">{{ $val['name'] }}</option>
                                @endforeach
                            </select>
                            <span class="help-block m-b-none">{{$errors->first('info.source')}}</span>
                        </div>
                        <label class="col-sm-2 control-label">首投：</label>
                        <div class="col-sm-2">
                            <select class="form-control m-b" name="info[fist_invest_status]">
                                @foreach( $isinvested as $key => $val)
                                    <option value="{{ $val['id'] }}">{{ $val['name'] }}</option>
                                @endforeach
                            </select>
                            <span class="help-block m-b-none">{{$errors->first('info.source')}}</span>
                        </div>
                        <label class="col-sm-2 control-label">性别：</label>
                        <div class="col-sm-2">
                            <select class="form-control m-b" name="info[sex]">
                                @foreach($sexs as $key=>$val)
                                    <option value="{{ $val['id'] }}">{{ $val['name'] }}</option>
                                @endforeach
                            </select>
                            <span class="help-block m-b-none">{{$errors->first('info.source')}}</span>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label">年龄&nbsp;&nbsp;&gt;=</label>
                        <div class="col-sm-4">
                            <div class="row">
                                <div class="col-md-4">
                                    <input type="text" name="info[min_age]" value=""
                                           placeholder="年龄" class="form-control">
                                    <span class="help-block m-b-none"></span>
                                </div>
                            </div>
                        </div>
                        <label class="col-sm-2 control-label">最小单笔投资金额 &nbsp;&nbsp;&gt;=</label>
                        <div class="col-sm-4">
                            <div class="row">
                                <div class="col-md-6">
                                    <input type="text" name="info[min_single_invest_num]" value=""
                                           placeholder="最小单笔投资金额" class="form-control">
                                    <span class="help-block m-b-none"></span>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label">最小总投资额 &nbsp;&nbsp;&gt;=</label>
                        <div class="col-sm-4">
                            <div class="row">
                                <div class="col-md-6">
                                    <input type="text" name="info[min_total_invest_num]" value=""
                                           placeholder="最小总投资额" class="form-control">
                                    <span class="help-block m-b-none"></span>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label" >等级：</label>
                        <div class="col-sm-10">
                            <div class="row">
                                <div class="col-sm-10">
                                    <fieldset>
                                        <legend >
                                            <small>
                                                <label class="checkbox-inline i-checks">
                                                    <input class="check-all"
                                                           type="checkbox" data-value=""
                                                    >
                                                    全选
                                                </label>
                                            </small>
                                        </legend>
                                        {{--  @foreach($permission['childs'] as $child)
                                              <label class="checkbox-inline i-checks">
                                                  <input class="check-all-child" type="checkbox" name="permissions[]" value="{{$child['id']}}"

                                                  >{{$child['label']}}</label>
                                          @endforeach--}}
                                    </fieldset>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm-6 col-sm-offset-2">
                            {{--开始发送开始--}}
                            <div class="modal inmodal"
                                 id="myModal-hb" tabindex="-1"
                                 role="dialog"
                                 aria-hidden="true"
                                 data-backdrop="static" data-keyboard="ture" >
                                <div class="modal-dialog ">
                                    <div class="modal-content animated bounceInRight">
                                        <div class="col-sm-12 _block-hb _block-quan">
                                            <div class="contact-box border-hb">
                                                <div class="row">
                                                    <div class="col-sm-12">
                                                        <div class="ibox ">
                                                            <div id="what-doing" class="text-center font-bold">正在发送</div>
                                                            <div class="sk-spinner sk-spinner-three-bounce">
                                                                <div class="sk-bounce1"></div>
                                                                <div class="sk-bounce2"></div>
                                                                <div class="sk-bounce3"></div>
                                                            </div>
                                                        </div>

                                                        <div class="progress progress-striped active m-b-sm">
                                                            <div id="progress-now" style="width: 0%;" class="progress-bar"></div>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <div class="col-sm-4 col-sm-offset-2">
                                                        <button class="btn btn-white _close-modal" style="display: none" type="button" data-dismiss="modal">返回修改</button>
                                                    </div>
                                                    <div class="col-sm-4 col-sm-offset-2">
                                                        <button class="btn btn-primary" id="btn-add-hb" style="display: none" type="button">确定</button>
                                                    </div>
                                                </div>
                                                <div class="clearfix"></div>
                                            </div>
                                        </div>

                                    </div>
                                </div>
                            </div>
                            {{----}}
                        </div>
                    </div>

                    <div class="hr-line-dashed"></div>
                    <div class="form-group ">
                        <div class="col-sm-12">
                            <div class="col-sm-1" id="loading" style="display: none">
                                <div class="sk-spinner sk-spinner-wave">
                                    <div class="sk-rect1"></div>
                                    <div class="sk-rect2"></div>
                                    <div class="sk-rect3"></div>
                                    <div class="sk-rect4"></div>
                                    <div class="sk-rect5"></div>
                                </div>
                            </div>
                            <input name="count" value="" type="hidden">

                            <label class="col-sm-3 text-danger" id="res-count"></label>
                        </div>
                    </div>


                    <div class="hr-line-dashed"></div>
                    <div class="form-group">
                        <div class="col-sm-4 col-sm-offset-2">
                            <button class="btn btn-primary" type="button" id="btn-filter" >筛选</button>
                            <button class="btn btn-primary" type="button" id="btn-send" {{--data-toggle="modal" data-target="#myModal-hb"--}}>开始发送</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>

@endsection

@section('javascript')
    <script src="/js/plugins/sweetalert/sweetalert.min.js"></script>
    <script>
        $(document).ready(function () {
            //日期范围限制
            var start = {
                elem: '#start',
                format: 'YYYY-MM-DD',
                //  min: laydate.now(), //设定最小日期为当前日期
                max: '2099-06-16 23:59:59', //最大日期
                istime: true,
                istoday: false,
                choose: function (datas) {
                    end.min = datas; //开始日选好后，重置结束日的最小日期
                    end.start = datas //将结束日的初始值设定为开始日
                }
            };
            var end = {
                elem: '#end',
                format: 'YYYY-MM-DD',
                min: laydate.now(),
                max: '2099-06-16 23:59:59',
                istime: true,
                istoday: false,
                choose: function (datas) {
                    start.max = datas; //结束日选好后，重置开始日的最大日期
                }
            };
            $('#start').click(function () {
                laydate(start);
            });$('#end').click(function () {
                laydate(end);
            });




            $('.i-checks').iCheck({
                checkboxClass: 'icheckbox_square-green',
                radioClass: 'iradio_square-green',
            });

            $("textarea[name='sms-content']").keydown(function () {
                countTextNum()
            });
            $("textarea[name='sms-content']").keyup(function () {
                countTextNum()
            });
            function countTextNum() {
                var counts = $("textarea[name='sms-content']").val().length;
                $('#text-count').html("已经输入"+counts +"字");
            }

            //筛选button
            $("#btn-filter").click(function () {
                getFilerCount(true);
            });

            function getFilerCount(async) {
                var formdate = $("form").serialize();
                var count ;
                $.ajax({
                    type:'get',
                    url:'/ceb/users/filterGetall',
                    data:formdate,
                    async:async,//取消异步请求
                    beforeSend:function(){
                        $('#res-count').html('');
                        $("#loading").show();
                    },
                    success:function (data) {
                        count= data.data.count;
                        $("input[name='count']").val(count);
                        $('#res-count').html(data.data.count+"人符合结果");
                    },
                    complete:function () {
                        $("#loading").hide();
                    }
                });
                return count;
            }

            $("#btn-send").click(function () {
                var dom_content = $("textarea[name='sms-content']");
                if( dom_content.val() == 0){
                    swal({
                        title: "短信内容不能为空",
                        type: "warning",
                        text: ""
                    });
                    return false;
                }
                $("#myModal-hb").modal('show');
                count = getFilerCount(false);
                if(count == 0 || count == null){
                    swal({
                        title: "筛选数量为空",
                        type: "warning",
                        text: ""
                    });
                    $("#myModal-hb").modal('hide');
                    return false;
                }
                var entity = createrEntity(false);
                if(entity.code != 1){
                    swal({
                        title: "创建实例失败，返回重新发送！",
                        type: "warning",
                        text: ""
                    });
                    $("#myModal-hb").modal('hide');
                    return false;
                }

                var i=1;
                var pageCount=entity.data.pages;
                var loop = function () {
                    if(i>pageCount){
                        $("._close-modal").trigger('click');
                        swal({
                            title: "发送完成",
                            type: "success"
                        });
                        return;
                    }//退出循环

                    i++;
                    loadUserInfo(false,i,entity.data.pageSize,entity.data.send_sms_id)
                    //下一步循环
                    window.setTimeout(loop, 1000);
                }
                window.setTimeout(loop, 1000);
                return false;
            });


            function createrEntity(async) {
                var formdate = $("form").serialize();
                var entity;
                $.ajax({
                    type:'post',
                    url:'/ceb/sms/doCreate',
                    data:formdate,
                    async:async,//取消异步请求
                    success:function (data) {
                        entity = data;
                    }
                });
                return entity;
            }
            function loadUserInfo(async,page,pageSize,sendId) {
                var formdate = $("form").serialize();
                formdate= formdate +'&page=' +page +'&pageSize=' + pageSize+'&sendId=' + sendId ;
                var progress;
                $.ajax({
                    type:'post',
                    url:'/ceb/sms/loadUserInfo',
                    data:formdate,
                    async:async,//取消异步请求
                    success:function (data) {
                        progress = data.data.progress+'%'
                    }
                });
                $("#progress-now").width( progress);
            }

            $('.check-all').on('ifChecked', function (event) {
                $(".check-all-child").iCheck('check');
            });

            $('.check-all').on('ifUnchecked', function (event) {
                $(".check-all-child").iCheck('uncheck');
            });

        });
    </script>

@endsection

